from pywinauto.application import Application
from PIL import ImageGrab
import time
import pyautogui
import os
import win32gui
import win32con
import  ctypes
import sys
import pygetwindow as gw
apps=[
    # "button_exe_0000000449",
    ["C:\\Users\\wanlei\\Desktop\\button_recorder\\images","button_exe_0000000257.exe"],
    # "button_test_exe12",
    # "button_test_exe13",
    # "button_test_exe14",
    # "button_test_exe15",
]



def runOneApp(name):
    name_dict={}
    path=os.path.join(name[0],name[1])
    print(path)
    app = Application(backend='uia').start(path)
    parent=win32gui.GetForegroundWindow()
    classN=win32gui.GetClassName(parent)
    print(classN)
    hwnd=win32gui.FindWindowEx(parent,None,classN,None)


    time.sleep(10)


    # print(w.GetWindowText(w.GetForegroundWindow()))
    # bbox=win32gui.GetWindowRect(parent)
    bbox = win32gui.GetWindowRect(parent)

    x,y,w,h=bbox[0],bbox[1],bbox[2]-bbox[0],bbox[3]-bbox[1]
    bbox=[x+1,y-1,w,h]
    print(x,y,w,h)
    mid_x,mid_y=bbox[0]+bbox[2]/2,bbox[1]+bbox[3]/2

    count = 0
    f=open(".\\steps\\"+name[1][:-4],"w")
    while True:
        try:
            time.sleep(2)
            sons = []
            win32gui.EnumChildWindows(parent, lambda hwnd, param: param.append(hwnd), sons)
            button_name=win32gui.GetWindowText(sons[0])
            win32gui.ShowWindow(sons[0], win32con.SW_SHOWMAXIMIZED)

            # button_name = window.descendants()[0].window_text()
        except Exception as e:
            print(e)
            print("end .....")
            win32gui.CloseWindow(parent)
            break
        else:
            if button_name in name_dict.keys():
                print(button_name+" click failed")
                # window.close()
                win32gui.CloseWindow(parent)
                return
            name_dict[button_name]=True
            imagedir="./images/"+name[1][:-4]
            if not os.path.exists(imagedir):
                os.makedirs(imagedir)
            im=pyautogui.screenshot(region=bbox)
            #im = ImageGrab.grab((150, 150, 800, 600))
            im.save(imagedir+"/" + str(count) + ".png")
            print(button_name+"....")
            f.write(button_name+"\n")
            count += 1
            # pyautogui.click(300, 300)

            # DEFSPLITBUTTON can not by click
            try:
                window[button_name].click()
            except:
                pyautogui.click(mid_x, mid_y)

    win32gui.CloseWindow(parent)
    f.close()

def get_all_exe(dir):
    for i,_,k in os.walk(dir):
        for e in k:
            path=os.path.join(i,e)
            print(path)
            apps.append([i,e])
if __name__=="__main__":
    # x,y,w,h=sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4]
    dir=sys.argv[1]
    get_all_exe(dir)
    for name in apps:
        try:
            runOneApp(name)
        except Exception as e:
            print(e)



